package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entiy.dto.Order;
import com.qf.entiy.po.LoginMerchant;
import com.qf.entiy.vo.OrderVo;
import com.qf.mapper.OrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderServcie extends ServiceImpl<OrderMapper, Order> {

    SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");

    @Transactional(readOnly = true)
    public Integer getBuyerNumByGoodsId(Long goodsId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Order::getGoodsId,goodsId);
        return this.count(wrapper);
    }

    @Transactional
    public void createOrder(Order order, LoginMerchant loginMerchant) {
        order.setOrderId(SIMPLE_DATE_FORMAT.format(new Date()).concat(loginMerchant.getId().toString()));
        order.setMerchantUserId(loginMerchant.getId());
        order.setPrice(1);
        order.setPayState(Order.payState.PENDING.getCode());

        this.save(order);
    }

    @Transactional(readOnly = true)
    public List<OrderVo> getOrderlistById(Long merchantUserId) {
        return this.baseMapper.getOrderListById(merchantUserId);
    }
}
